package com.lovely602.langchain4j.functioncalling.config;


import com.lovely602.langchain4j.functioncalling.service.FunctionAssistant;
import com.lovely602.langchain4j.functioncalling.service.WeatherHandler;
import dev.langchain4j.agent.tool.ToolSpecification;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.request.json.JsonObjectSchema;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.service.tool.ToolExecutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

/**
 *
 * @author lizhixing
 */
@Configuration
public class LLMConfig {

    @Autowired
    private WeatherHandler weatherHandler;

    @Bean
    public ChatModel chatModel() {
        return OpenAiChatModel.builder()
                .apiKey(System.getenv("AI_DASHSCOPE_API_KEY"))
                .modelName("qwen-plus")
                .baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
                .build();
    }


    /**
     *  第一组 Low Level Tool API
     *    https://docs.langchain4j.dev/tutorials/tools#low-level-tool-api
     */
    @Bean("invoicingAssistant")
    public FunctionAssistant invoicingAssistant(ChatModel chatModel) {

        JsonObjectSchema objectSchema = JsonObjectSchema
                .builder()
                .description("开具发票助手参数")
                .addStringProperty("companyName", "公司名称")
                .addStringProperty("dutyNumber", "税号序列")
                .addStringProperty("amount", "开票金额，保留两位有效数字")
                .build();

        ToolSpecification toolSpecification = ToolSpecification
                .builder()
                .name("开具发票助手")
                .description("根据用户提交的开票信息，开具发票")
                .parameters(objectSchema)
                .build();

        // 业务逻辑实现
        ToolExecutor toolExecutor = (toolExecutionRequest, memoryId) -> {

            String arguments = toolExecutionRequest.arguments();
            System.out.println("toolExecutionRequest.id ：" + toolExecutionRequest.id());
            System.out.println("toolExecutionRequest.name ：" + toolExecutionRequest.name());
            System.out.println("toolExecutionRequest.arguments ：" + arguments);

            return "开票成功";
        };

        return AiServices
                .builder(FunctionAssistant.class)
                .chatModel(chatModel)
                .chatMemory(MessageWindowChatMemory.withMaxMessages(50))
                .tools(Map.of(toolSpecification, toolExecutor)) // tools (Function calling)
                .build();
    }

    /**
     * 第二组 High Level Tool API
     * https://docs.langchain4j.dev/tutorials/tools#high-level-tool-api
     */
    @Bean("weatherAssistant")
    public FunctionAssistant weatherAssistant(ChatModel chatModel) {
        return AiServices.builder(FunctionAssistant.class)
                .chatModel(chatModel)
                .chatMemory(MessageWindowChatMemory.withMaxMessages(50))
                .tools(weatherHandler)
                .build();
    }

}
